//
//  LZTComposeBottomView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/17.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit

// 底部视图按钮的枚举
enum LZTComposeBottomViewType: Int {
    //图片
    case Picture = 0
    // @
    case Mention = 1
    // #
    case Trend = 2
    // 笑脸
    case Emoticon = 3
    // +
    case Add = 4
}

class LZTComposeBottomView: UIView {
    
    //
    var emoticonButton: UIButton?
    
    //声明一个闭包：用于告知外界的VC是哪一个按钮被点击，执行相对应的任务
    var closure: ((type: LZTComposeBottomViewType)->())?
    
    //定义一个属性 供外界设置 emoticonButton
    //true 代表当前显示的是 表情键盘  显示键盘图片
    //false 代表当前显示的是 系统键盘 显示为笑脸图片
    var isEmticon: Bool = false {
        
        didSet{
            let imgName: String
            if isEmticon {
                //表情键盘的显示时，展示 键盘图片
                imgName = "compose_keyboardbutton_background"
                
            }else {
                //系统键盘
                imgName = "compose_emoticonbutton_background"
            }
            
            //设置图片
            emoticonButton?.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //按钮的点击事件
    func btnClick(btn: UIButton) {
        
        //closure-03
        closure?(type: LZTComposeBottomViewType(rawValue: btn.tag)!)
    }
    
    //设置UI
    func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        //创建按钮
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtons("compose_add_background",type: .Add)
        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
            
        }
        mentionButton.snp_makeConstraints { (make) in
            make.leading.equalTo(pictureButton.snp_trailing)
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) in
            make.top.trailing.bottom.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
    }
    
    //添加底部按钮
    func addChildButtons(imgName: String, type: LZTComposeBottomViewType) -> UIButton{
        
        let btn = UIButton()
        //设置tag 通过枚举名得到对应的枚举值
        btn.tag = type.rawValue
        //添加点击事件
        btn.addTarget(self, action: #selector(LZTComposeBottomView.btnClick), forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        addSubview(btn)
        return btn
    }
    
}
